﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using SoSlam.Common.Communication;

namespace SoSlam.Common.ClientLogic
{
    public class Bird : DrawableGameComponent
    {
        private World _world;
        private TrackingCamera _camera;

        private AnimatedSprite _birdSprite;

        private TimeSpan _scaleAnimationDuration = TimeSpan.FromMilliseconds(300);
        private TimeSpan _lastScaleChange = TimeSpan.Zero;

        public int BirdId { get; set; }

        public bool IsHit { get; set; }

        public Vector2 Position { get; set; }

        public float Width { get { return _birdSprite.Width; } }
        public float Height { get { return _birdSprite.Height; } }

        public Bird(Game game, World world, TrackingCamera camera)
            : base(game)
        {
            _world = world;
            _camera = camera;
        }

        public override void Initialize()
        {
            _birdSprite = new AnimatedSprite(Game, new AnimationDefinition
            {
                AssetName = "Sprites/bird",
                FrameRate = 10,
                FrameSize = new Point(Constants.BIRD_WIDTH, Constants.BIRD_HEIGHT),
                Loop = true,
                NbFrames = new Point(4, 1)
            });

            _birdSprite.Initialize();

            base.Initialize();
        }

        protected override void LoadContent()
        {
            _birdSprite.LoadContent();

            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            if (_birdSprite.Scale <= 0)
                return;

            _birdSprite.Update(gameTime);
            _birdSprite.Position = Position - _camera.Position;

            if (IsHit
                && _lastScaleChange <= _scaleAnimationDuration)
            {
                _lastScaleChange += gameTime.ElapsedGameTime;
                _birdSprite.Scale = 1 - (float)Math.Max(_lastScaleChange.TotalSeconds / _scaleAnimationDuration.TotalSeconds, 0);
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            _birdSprite.Draw(gameTime, true);

            base.Draw(gameTime);
        }
    }
}
